package com.driver.fordriver.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationData;
import com.driver.fordriver.R;
import com.driver.fordriver.application.MyApplication;
import com.driver.fordriver.util.XywUtil;

/**
 * Created by XuYeWen on 2017/4/27.
 */

public class MapFragment extends BaseFragment {

    private MapView mapView;
    private BaiduMap mBaiduMap;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_map_layout, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mapView= (MapView) view.findViewById(R.id.bdmapView);
        mapView.onCreate(getActivity(),savedInstanceState);
        mBaiduMap=mapView.getMap();
        init2();
    }

    private void init2() {
        // 开启定位图层
        mBaiduMap.setMyLocationEnabled(true);
        // 定位初始化
        XywUtil.log("init2=");
        LocationClient mLocClient = new LocationClient(getActivity());
        mLocClient.registerLocationListener(new BDAbstractLocationListener() {
            @Override
            public void onReceiveLocation(BDLocation location) {
               double latitude = location.getLatitude();    //获取纬度信息
                double longitude = location.getLongitude();    //获取经度信息
               float radius = location.getRadius();    //获取定位精度，默认值为0.0f
                XywUtil.log(latitude+"--"+longitude+"--"+radius);
            }
        });
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true); // 打开gps
        option.setLocationMode(LocationClientOption.LocationMode.Battery_Saving);
        option.setScanSpan(2000);
        option.setCoorType("bd09ll"); // 设置坐标类型
        mLocClient.setLocOption(option);
        mLocClient.start();
    }

    protected void init() {
        // 开启定位图层
        mBaiduMap.setMyLocationEnabled(true);

// 构造定位数据
        MyLocationData locData = new MyLocationData.Builder()
                .accuracy(((MyApplication)getActivity().getApplication()).getRadius())
                // 此处设置开发者获取到的方向信息，顺时针0-360
                .direction(100).latitude(((MyApplication)getActivity().getApplication()).getLatitude())
                .longitude(((MyApplication)getActivity().getApplication()).getLongitude()).build();

// 设置定位数据
        mBaiduMap.setMyLocationData(locData);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    @Override
    public void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mapView.onResume();
    }
    @Override
    public void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mapView.onPause();
    }
}

